﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/5/29 11:40:00
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace CommonLibrary
{
    /// <summary>
    /// PLC地址监控窗体
    /// </summary>
    public partial class FormAddressMonitor : HWFrmWithTitle
    {
        private readonly string CONFIG_PATH = Path.Combine(CommonHelper.COMMON_CONFIG_DIR, "PLCAddressMonitor.json");
        private Dictionary<string, object> mDic = new Dictionary<string, object>();
        private List<PLCTagSetting> mAddressList = new List<PLCTagSetting>();
        private object LOCK_HANDLE_DIR = new object();
        /// <summary>
        /// 构造函数
        /// </summary>
        public FormAddressMonitor()
        {
            InitializeComponent();
        }
        private void FormAddressMonitor_Load(object sender, EventArgs e)
        {
            LoadConfig();
            InitColumns();
            InitDataColumns();
            this.timer.Start();
        }
        private void LoadConfig()
        {
            this.mAddressList = CommonHelper.GetJsonListFromFile<PLCTagSetting>(CONFIG_PATH);
            CommonHelper.SaveToJsonFile(this.mAddressList, CONFIG_PATH);
        }
        private void InitDataColumns()
        {
            Dictionary<string, object> temp = new Dictionary<string, object>();
            foreach (var t in this.mAddressList)
            {
                temp.Add(t.ORMName, "");
            }
            int len = temp.Keys.Count;
            var split = len > 2;
            var splitValue = split ? len / 2 : int.MaxValue;
            int index = 0;
            int newRowIndex = -1;
            foreach (var d in temp)
            {
                newRowIndex = index > splitValue ? this.dataGridView2.Rows.Add(1) : this.dataGridView1.Rows.Add(1);
                var row = index > splitValue ? this.dataGridView2.Rows[newRowIndex] : this.dataGridView1.Rows[newRowIndex];
                row.Cells["Name"].Value = d.Key;
                row.Cells["Value"].Value = d.Value;
                row.Tag = d.Key;
                index++;
            }
            this.dataGridView1.ClearSelection();
            this.dataGridView2.ClearSelection();
        }
        private void InitColumns()
        {
            CommonHelper.CreateTextGridColumn(this.dataGridView1, "Name", "名称");
            CommonHelper.CreateTextGridColumn(this.dataGridView1, "Value", "实时值");
            this.dataGridView1.DoubleBufferedDataGirdView(true);
            this.dataGridView1.InitStyle();
            CommonHelper.CreateTextGridColumn(this.dataGridView2, "Name", "名称");
            CommonHelper.CreateTextGridColumn(this.dataGridView2, "Value", "实时值");
            this.dataGridView2.DoubleBufferedDataGirdView(true);
            this.dataGridView2.InitStyle();
        }
        private void FormAddressMonitor_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.timer.Stop();
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            if (APPInstance.IsPLCConnected)
            {
                lock (LOCK_HANDLE_DIR)
                {
                    this.mDic = APPInstance.PLC.BatchRead(this.mAddressList);
                }
            }
            ShowValues();
        }
        private void UpdateValues(DataGridView dataGridView)
        {
            if (dataGridView.Rows.Count > 0)
            {
                for (var i = 0; i < dataGridView.Rows.Count; i++)
                {
                    var row = dataGridView.Rows[i];
                    var tag = row.Tag;
                    if (tag != null)
                    {
                        string key = tag.ToString();
                        if (this.mDic.ContainsKey(key))
                        {
                            object obj = this.mDic[key];
                            object value = row.Cells["Value"].Value;
                            if (obj != null && value != null && obj.ToString() != value.ToString())
                            {
                                row.Cells["Value"].Style.BackColor = Color.Red;
                            }
                            else
                            {
                                row.Cells["Value"].Style.BackColor = Color.WhiteSmoke;
                            }
                            row.Cells["Value"].Value = obj;
                        }
                    }
                }
            }
        }
        private void ShowValues()
        {
            lock (LOCK_HANDLE_DIR)
            {
                if (this.mDic != null && this.mDic.Keys.Count > 0)
                {
                    UpdateValues(this.dataGridView1);
                    UpdateValues(this.dataGridView2);
                }
            }
        }
    }
}
